package com.ydd.conference.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ydd.conference.R;
import com.ydd.conference.util.StringUtils;

public class SingleVoteResultActivity extends BaseActivity {

    private TextView agreeText;
    private TextView againstText;
    private TextView abstainText;
    private TextView missText;

    private TextView result_one, result_three, result_two, result_four;

    private TextView final_result, final_result_pit, result_title;

    private String agreeNum;
    private String againstNum;
    private String abstainNum;
    private String missNum;
    private int width;
    private int height;

    private String subject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_vote);
        width = getWindowManager().getDefaultDisplay().getWidth();
        height = getWindowManager().getDefaultDisplay().getHeight();
        initData();
        initView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initData();
        initView();
    }

    private void initData() {
        agreeNum = getIntent().getStringExtra("agreeNum");
        againstNum = getIntent().getStringExtra("againstNum");
        abstainNum = getIntent().getStringExtra("abstainNum");
        missNum = getIntent().getStringExtra("missNum");
        subject = getIntent().getStringExtra("subject");
    }

    private void initView() {
        agreeText = (TextView) findViewById(R.id.agreeText);
        result_title = (TextView) findViewById(R.id.result_title);
        againstText = (TextView) findViewById(R.id.againstText);
        abstainText = (TextView) findViewById(R.id.abstainText);
        missText = (TextView) findViewById(R.id.missText);

        result_one = (TextView) findViewById(R.id.result_one);
        result_three = (TextView) findViewById(R.id.result_three);
        result_two = (TextView) findViewById(R.id.result_two);
        result_four = (TextView) findViewById(R.id.result_four);
        final_result = (TextView) findViewById(R.id.final_result);
        final_result_pit = (TextView) findViewById(R.id.final_result_pit);

        if (!TextUtils.isEmpty(subject) && subject.contains("满意")) {
            result_title.setText("测评结果");
            result_one.setText("满意");
            result_two.setText("基本满意");
            result_three.setText("不满意");
            result_four.setVisibility(View.VISIBLE);

            agreeText.setText(agreeNum);
            againstText.setText(abstainNum);
            abstainText.setText(againstNum);
            //判断三个票数中哪个最大
            try {
                int satisfation_Num = Integer.parseInt(agreeNum);
                int almost_satisfation_Num = Integer.parseInt(againstNum);
                int no_satisfation_Num = Integer.parseInt(abstainNum);
                int miss_Num = Integer.parseInt(missNum);
                int total = satisfation_Num + almost_satisfation_Num + no_satisfation_Num + miss_Num;

                if (satisfation_Num >= (total * 2 / 3)) {
                    final_result.setText("满意");
                } else if ((satisfation_Num + almost_satisfation_Num) <= total / 2) {
                    final_result.setText("不满意");
                } else {
                    final_result.setText("基本满意");
                }

//                if (satisfation_Num + no_satisfation_Num > total * 2 / 3 && satisfation_Num > total / 2) {
//                    final_result.setText("满意");
//                } else if ((satisfation_Num + no_satisfation_Num <= total / 2) || (satisfation_Num + no_satisfation_Num >= total / 2 && almost_satisfation_Num > total / 3)) {
//                    final_result.setText("不满意");
//                } else {
//                    final_result.setText("基本满意");
//                }

//                if (satisfation_Num >= almost_satisfation_Num && satisfation_Num >= no_satisfation_Num) {
//                    final_result.setText("满意");
//                } else if (almost_satisfation_Num >= no_satisfation_Num) {
//                    final_result.setText("基本满意");
//                } else {
//                    final_result.setText("不满意");
//                }
            } catch (Exception e) {

            }

            final_result_pit.setVisibility(View.VISIBLE);
            final_result.setVisibility(View.VISIBLE);

        } else {
            result_title.setText("表决结果");

            result_one.setText("赞成");
            result_two.setText("反对");
            result_three.setText("弃权");
            againstText.setText(againstNum);
            agreeText.setText(agreeNum);
            abstainText.setText(abstainNum);
            result_four.setVisibility(View.GONE);
            final_result_pit.setVisibility(View.GONE);
            final_result.setVisibility(View.GONE);
        }


        //TODO by ranfi
        if (StringUtils.isNotEmpty(missNum) && Integer.parseInt(missNum) > 0) {
            missText.setVisibility(View.VISIBLE);
            missText.setText("(有 " + missNum + " 人未按键)");
        } else {
            missText.setVisibility(View.GONE);
        }
    }

//    @Override
//    protected void onPause() {
//        super.onPause();
//        finish();
//    }

    public static void actionStart(Context context, String agreeNum, String againstNum, String abstainNum, String missNum, String subject) {
        Intent intent = new Intent(context, SingleVoteResultActivity.class);
        intent.putExtra("agreeNum", agreeNum);
        intent.putExtra("againstNum", againstNum);
        intent.putExtra("abstainNum", abstainNum);
        intent.putExtra("missNum", missNum);
        intent.putExtra("subject", subject);
        context.startActivity(intent);
    }


}
